﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class ComponentRatioViewModel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ComponentViewModel mComponent = null;
        private double mArea = 0.0;
        public ComponentViewModel Component
        {
            get { return mComponent; }
            set
            {
                mComponent = value;
                RaisePropertyChanged("Component");
            }
        }
        public double Area
        {
            get { return mArea; }
            set
            {
                mArea = value;
                RaisePropertyChanged("Ratio");
            }
        }

        public ComponentRatioViewModel() { }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null == doc) return _element;
            if (null != Component)
            {
                _element = doc.CreateElement("Component");

                XmlAttribute _name = doc.CreateAttribute("Name");
                _name.Value = Component.Name;
                _element.Attributes.Append(_name);

                XmlAttribute _rate = doc.CreateAttribute("Area");
                _rate.Value = this.Area.ToString();
                _element.Attributes.Append(_rate);
            }
            return _element;
        }
        public void Import(XmlElement element)
        {
            if (null == element) return;
            if ("Component" == element.Name)
            {
                IEnumerable<ComponentViewModel> _components = ComponentsViewModel.GetInstance().Components;
                foreach (ComponentViewModel _component in _components)
                {
                    if (element.Attributes["Name"].Value == _component.Name)
                    {
                        this.Component = _component;
                        break;
                    }
                }
                this.Area = double.Parse(element.Attributes["Area"].Value);
            }
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}